<style>
    .ssf_list{
        text-align: center;
        border-top: solid 1px #ddd;
        border-right: solid 1px #ddd;
        border-left: solid 1px #ddd;
    }
    .ssf_list table td{
        line-height: 25px;
        height: 25px;
    }
    .ssf_list .sb_head{background: #eee;height: 30px;line-height: 30px;}
    .ssf_list td, .ssf_list th{border-bottom: 1px solid #ddd;border-right: 1px solid #ddd;}
    .ssf_list .sb tr:last-child td{border-bottom: none;}
    .ssf_list .sb{
        margin: auto;
        width: 100%;
        text-align: center;
    }
    .price{color: red;font-size: 14px;}
    .ssf_list td:last-child, .ssf_list th:last-child{border-right: none;}
</style>
<div>
    <ul style="list-style:none;">
        <li>签约时间：<?php echo $business->buy_date; ?> 合同到期时间：<?php echo $business->over_date; ?> 最低服务费：<?php echo $business->min_sprice; ?> 每人服务费：<?php echo $business->unit_price; ?></li>
        <li>电话：<?php echo $business->tel; ?></li>
        <li>联系人：<?php echo $business->linkman; ?></li>
    </ul>
    <p style="background-color:#ddd;margin-bottom: 10px;padding: 5px;"><?php echo str_replace("\r\n", '<br/>', $business->comment_info); ?></p>
</div>
<div style="overflow-x: auto;">
    <table class="ssf_list" width="2000">
        <tr class="sb_head">
            <th width="60" style="width:80px!important;">姓名</th>
            <th width="150">身份证</th>
            <th width="60">工资</th>
            <th>社保项目</th>
            <th width="100">企业合计</th>
            <th width="100">个人合计</th>
            <th width="100">社保总额(￥)</th>
            <th width="100">余额</th>
            <th width="100">应付</th>

            <th width="100">操作</th>
            <th width="200">备注</th>
        </tr>
        <?php
        $customers = Customer::model()->findAll("b_id=$b_id AND c_status=1 AND over_date > '{$ssf_date}-01' AND buy_date <= '{$ssf_date}-01'"); //客户
        $all_money = 0;
        $u_type = 1; //用户类型
        if ($b_id > 1) {
            $all_money = CustomerRecharge::model()->getBalance($b_id, 2);
            $u_type = 2;
        }
        $max_colspan = 0;
        foreach ($customers as $user):
            if ($b_id == 1) {
                $all_money = CustomerRecharge::model()->getBalance($user->id, 1);
            }
            //员工社保项
            $ssfs = CustomerSsf::model()->getSsfByUid($user->id, $ssf_date);
            ?>
            <tr>
                <th><?php echo $user->name; ?></th>
                <th><?php echo $user->id_card; ?></th>
                <td><?php echo $user->wages; ?></td>
                <td>
                    <table cellpadding="5">
                        <tr>
                            <?php
                            $ssf_money = 0;
                            $business_total = 0;
                            $personal_total = 0;
                            foreach ($ssfs as $ssf):
                                $base_number = $ssf['is_change'] == 0 ? $ssf['base_number'] : ($user->wages > $ssf['base_number'] ? $user->wages : $ssf['base_number']); //基数
                                if ($base_number > 0) {
                                    $business = round($ssf['business'] / 100 * $base_number, 2); //企业缴纳
                                    $personal = round($ssf['personal'] / 100 * $base_number, 2); //个人缴纳
                                } else {
                                    $business = $ssf['business']; //企业缴纳
                                    $personal = $ssf['personal']; //个人缴纳
                                }
                                $business_total += $business;
                                $personal_total += $personal;

                                $ssf_money += $business + $personal;
                                ?>
                                <td width="200">
                                    <table class="sb" align="center">
                                        <tr>
                                            <th colspan="3" style="background: sandybrown;"><?php echo $ssf['ssf_name']; ?></th>
                                        </tr>
                                        <tr>
                                            <td>基数</td>
                                            <td><?php
                        echo $ssf['business'];
                        if ($base_number > 0) {
                            echo '%';
                        }
                                ?>(企业)</td>
                                            <td><?php
                                        echo $ssf['personal'];
                                        if ($base_number > 0) {
                                            echo '%';
                                        }
                                ?>(个人)</td>
                                        </tr>
                                        <tr>
                                            <td><?php echo $base_number; ?></td>
                                            <td><?php echo $business; ?></td>
                                            <td><?php echo $personal; ?></td>
                                        </tr>
                                    </table>
                                </td>
                                <?php
                            endforeach;
                            if ($colspan > $max_colspan)
                                $max_colspan = $colspan;
                            ?>
                        </tr>
                    </table>
                </td>
                <td class="price"><?php echo $business_total; ?></td>
                <td class="price"><?php echo $personal_total; ?></td>
                <td class="price"><?php echo $ssf_money; ?></td>
                <td><?php echo $all_money; ?></td>
                <td class="price"><?php echo $ssf_money; ?></td>

                <td style="padding-left:5px;">
                    <?php
                    $ssf_report = SsfReport::model()->findByAttributes(array(
                        'u_id' => $user->id,
                        'ssf_date' => $ssf_date
                            ));

                    if (isset($ssf_report) && round($ssf_report->paid_money) >= round($ssf_money)) {
                        echo '<span style="color:blue">已扣款</span>';
                    } else {
                        if ($all_money > $ssf_money):
                            ?>
                            <a class="gradient-btn" href="javascript:koukuan(<?php echo $user->id ?>, <?php echo $ssf_money; ?>)">扣款</a>
                            <?php
                        else:
                            switch ($u_type) {
                                case 1:
                                    echo CHtml::link('余额不足', Yii::app()->createUrl("customerRecharge/create", array("u_id" => $user->id, "u_type" => "1")), array('class' => 'gradient-btn'));
                                    break;
                                case 2:
                                    echo CHtml::link('余额不足', Yii::app()->createUrl("customerRecharge/create", array("u_id" => $b_id, "u_type" => "2")), array('class' => 'gradient-btn'));
                                    break;
                            }

                        endif;
                    }
                    ?>
                </td>
                <td><?php echo $user->comment_info; ?></td>
            </tr>
            <?php
        endforeach;
        ?>

    </table>
</div>

<script>
    function koukuan(u_id, ssf_money){
        $.post("<?php echo Yii::app()->createUrl('ssfReport/api'); ?>", {action:"koukuan", u_id: u_id, ssf_money: ssf_money, ssf_date:"<?php echo $ssf_date; ?>", u_type:"<?php echo $u_type; ?>"}, function(data){
            if(data == 1){
                alert('扣款成功！');
                location.reload();
            }else{
                alert(data);
            }
        });
    }
</script>